package com.chd.ledger_server.controller;

import com.chd.ledger_server.entity.PipeStandard;
import com.chd.ledger_server.service.PipeStandardService;
import com.chd.ledger_server.domain.PageDto;
import com.chd.ledger_server.domain.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 管材标准表(PipeStandard)表控制层
 *
 * @author makejava
 * @since 2025-07-07 09:25:31
 */
@RestController
@RequestMapping("/pipeStandard")
@CrossOrigin
public class PipeStandardController {
    /**
     * 服务对象
     */
    @Resource
    private PipeStandardService pipeStandardService;

    /**
     * 分页查询
     *
     * @param pageDto 分页请求实体，仅包含PipeStandard实体+分页数据
     * @return PipeStandard列表
     */
    @PostMapping("/queryByPage")
    public Result queryByPage(@RequestBody PageDto pageDto) {
        PageInfo list = pipeStandardService.queryAllPipeStandardsByPage(pageDto);
        if (list.getList().isEmpty()) return Result.buildFail("未查到相关信息！");
        return Result.buildSuccess(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryById")
    public Result queryById(Integer id) {
        PipeStandard entity = this.pipeStandardService.queryById(id);
        if (entity == null) {
            return Result.buildFail("未找到记录");
        }
        return Result.buildSuccess(entity);
    }

    /**
     * 新增数据
     *
     * @param pipeStandard 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody PipeStandard pipeStandard) {
        PipeStandard result = this.pipeStandardService.insert(pipeStandard);
        return Result.buildSuccess(result);
    }

    /**
     * 编辑数据
     *
     * @param pipeStandard 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody PipeStandard pipeStandard) {
        PipeStandard result = this.pipeStandardService.update(pipeStandard);
        if (result == null) {
            return Result.buildFail("更新失败，记录不存在");
        }
        return Result.buildSuccess(result);
    }

    /**
     * 删除数据
     *
     * @param pipeStaId 主键
     * @return 删除是否成功
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer pipeStaId) {
        boolean success = this.pipeStandardService.deleteById(pipeStaId);
        if (success) {
            return Result.buildSuccess("删除成功");
        } else {
            return Result.buildFail("删除失败，记录不存在");
        }
    }
}
